package com.anhuanjia.tools.eao.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import com.anhuanjia.tools.eao.beanvo.EaoXlsVO;
import com.anhuanjia.tools.eao.util.ExportExcelUtil;
import com.anhuanjia.tools.eao.util.PrintWordUtil;
import com.efastsoft.framework.Config;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.core.handler.EAction;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.utility.IOUtil;

/**
 * 打印操作
 * 
 * @author Administrator
 *
 */
@Alias(value = "print")
public class PrintUI extends UIFacade {
	PrintWordUtil printutil = new PrintWordUtil();
	//protected String Url ="/www/wwwroot/xapi.letqvc.cn/eao/";
	//protected String Url ="//root/local/kencery/tomcat/webapps/eao/";
	//protected String Url ="C:\\Users\\Administrator.PC-201807211757\\git\\data\\WebRoot\\";
	protected String Url =Config.getContextRealPath()+"eao/";
	public static String getUUID(){ 
	        String s =UUID.randomUUID().toString().trim().replaceAll("-", ""); 
	       
	        return s; 
	 }
	
	/**
	 * 生成基本项目信息表
	 */
	@Priv(loginType = LoginType.Member)
	public void printProject(EAction za) {
		String pid = $V("ProjectId");////root/local/kencery/tomcat/webapps/eao/download/
		String inputUrl = Url+"template/项目基本信息表模板.docx";
		String uuid =getUUID();
		String outputUrl =Url+ "download/"+uuid+".docx";
		auto(pid,inputUrl,outputUrl,null);
		download(za,outputUrl,"项目基本信息表","docx");
	}

	/**
	 * 生成资料收集模文档
	 */
	@Priv(loginType = LoginType.Member)
	public void printCollect(EAction za) {
		String pid = $V("ProjectId");
		String inputUrl = Url+"template/资料收集表模板.docx";
		String uuid =getUUID();
		String outputUrl =Url+ "download/"+uuid+".docx";
		List<List<String[]>> testList = new ArrayList<List<String[]>>();
		List<String[]> list = printutil.getStaffList(pid);
		testList.add(list);
		auto(pid,inputUrl,outputUrl,testList);
		download(za,outputUrl,"资料收集表","docx");
	}
	
	/**
	 * 生成踏勘文档
	 */
	@Priv(loginType = LoginType.Member)
	public void printSurver(EAction za) {
		String pid = $V("ProjectId");
		String inputUrl = Url+"template/现场踏勘表模板.docx";
		String uuid =getUUID();
		String outputUrl =Url+ "download/"+uuid+".docx";
		List<List<String[]>> testList = new ArrayList<List<String[]>>();
		List<String[]> list1 = printutil.getBuildingContent(pid);
		testList.add(list1);
		List<String[]> list2 = printutil.getProductionEquipment(pid);
		testList.add(list2);
		List<String[]> list3 = printutil.getSensitive(pid);
		testList.add(list3);
		List<String[]> list4 = printutil.getSurroundCompany(pid);
		testList.add(list4);
		auto(pid,inputUrl,outputUrl,testList);
		download(za,outputUrl,"现场踏勘表","docx");
	}
	/**
	 * 生成环境质量及监测情况文档
	 */
	@Priv(loginType = LoginType.Member)
	public void printMonitor(EAction za) {
		String pid = $V("ProjectId");
		String inputUrl = Url+"template/环境质量及监测情况表模板.docx";
		String uuid =getUUID();
		String outputUrl = Url+ "download/"+uuid+".docx";
		auto(pid,inputUrl,outputUrl,null);
		download(za,outputUrl,"环境质量及监测情况表","docx");
	}
	/**
	 * 报告编制情况表模板
	 */
	@Priv(loginType = LoginType.Member)
	public void printFormation(EAction za) {
		String pid = $V("ProjectId");
		String inputUrl =Url+"template/报告编制情况表模板.docx";
		String uuid =getUUID();
		String outputUrl = Url+ "download/"+uuid+".docx";
		auto(pid,inputUrl,outputUrl,null);
		download(za,outputUrl,"报告编制情况表","docx");
	}
	
	/**
	 * 报告送审表模板
	 */
	@Priv(loginType = LoginType.Member)
	public void printAudit(EAction za) {
		String pid = $V("ProjectId");
		String inputUrl =Url+"template/报告送审表模板.docx";
		String uuid =getUUID();
		String outputUrl = Url+ "download/"+uuid+".docx";
		auto(pid,inputUrl,outputUrl,null);
		download(za,outputUrl,"报告送审表","docx");
	}
	/**
	 * 技术评估表模板
	 * @param za
	 */
	@Priv(loginType = LoginType.Member)
	public void printAppraise(EAction za) {
		String pid = $V("ProjectId");
		String inputUrl = Url+"template/技术评估表模板.docx";
		String uuid =getUUID();
		String outputUrl = Url+ "download/"+uuid+".docx";
		List<List<String[]>> testList = new ArrayList<List<String[]>>();
		List<String[]> list = printutil.getAppraise(pid);
		testList.add(list);
		auto(pid,inputUrl,outputUrl,testList);
		download(za,outputUrl,"技术评估表","docx");
	}
	/**
	 * 技术评估表-修改单模板
	 * @param za
	 */
	@Priv(loginType = LoginType.Member)
	public void printAppraiseChange(EAction za) {
		String pid = $V("ProjectId");
		String inputUrl = Url+"template/技术评估-修改表模板.docx";
		String uuid =getUUID();
		String outputUrl =Url+ "download/"+uuid+".docx";
		List<List<String[]>> testList = new ArrayList<List<String[]>>();
		List<String[]> list = printutil.getAppraise(pid);
		testList.add(list);
		auto2(pid,inputUrl,outputUrl,testList);
		download(za,outputUrl,"技术评估-修改表","docx");
	}
	
	/**
	 * 报告报批表模板
	 */
	@Priv(loginType = LoginType.Member)
	public void printBatch(EAction za) {
		String pid = $V("ProjectId");
		String inputUrl =Url+"template/报告报批表模板.docx";
		String uuid =getUUID();
		String outputUrl =Url+ "download/"+uuid+".docx";
		auto(pid,inputUrl,outputUrl,null);
		download(za,outputUrl,"报告报批表","docx");
	}
	/**
	 * 报告公示模板
	 * @param za
	 */
	@Priv(loginType = LoginType.Member)
	public void printPublicShow(EAction za) {
		String pid = $V("ProjectId");
		String inputUrl = Url+"template/报告公示表模板.docx";
		String uuid =getUUID();
		String outputUrl = Url+ "download/"+uuid+".docx";
		List<List<String[]>> testList = new ArrayList<List<String[]>>();
		List<String[]> list = printutil.getPublicQuestions(pid);
		testList.add(list);
		auto(pid,inputUrl,outputUrl,testList);
		download(za,outputUrl,"报告公示表","docx");
	}
	
	/**
	 * 报告报告存档及资料清单模板
	 */
	@Priv(loginType = LoginType.Member)
	public void printArchives(EAction za) {
		String pid = $V("ProjectId");
		String inputUrl = Url+"template/报告存档及资料清单模板.docx";
		String uuid =getUUID();
		String outputUrl = Url+ "download/"+uuid+".docx";
		auto(pid,inputUrl,outputUrl);
		download(za,outputUrl,"报告存档及资料清单","docx");
	}
	/**
	 * 审核报告修改模板
	 * @param za
	 */
	@Priv(loginType = LoginType.Member)
	public void printAuditForm(EAction za) {
		String pid = $V("ProjectId");
		String atype = $V("Atype");
		Map<String, String> amap =printutil.getAudit(pid, atype);
		String title = amap.get("title");
		String aid = amap.get("aid");
		String inputUrl = Url+"template/审核意见及修改表模板.docx";
		String uuid =getUUID();
		String outputUrl = Url+ "download/"+uuid+".docx";
		List<List<String[]>> testList = new ArrayList<List<String[]>>();
		List<String[]> list = printutil.getAuditReply(aid);
		testList.add(list);
		auto(pid,atype,inputUrl,outputUrl,testList);
		download(za,outputUrl,title,"docx");
	}
	
	/**
	 * 已审核项目表模板
	 * @param za
	 */
	@Priv(loginType = LoginType.Member)
	public void printAuditBill(EAction za) {
		String startTime = $V("StartTime");
		String endTime = $V("EndTime");
		int pageSize = this.Request.getInt("PageSize");
		int pageIndex = this.Request.getInt("PageIndex");
		String inputUrl = Url+"template/已审核项目表模板.docx";
		String uuid =getUUID();
		String outputUrl =Url+ "download/"+uuid+".docx";
		List<List<String[]>> testList = new ArrayList<List<String[]>>();
		List<String[]> list = printutil.findByAuditor(pageSize, pageIndex, startTime, endTime);
		if(list.isEmpty()) {
			f("打印失败");
		}
		testList.add(list);
		auto3(startTime,endTime,inputUrl,outputUrl,testList);
		download(za,outputUrl,"已审核项目表","docx");
	}
	
	/**
	 * 生成项目记录Excel
	 */
	@Priv(loginType = LoginType.Member)
	public void exportExcel(EAction za) {
		ExportExcelUtil<EaoXlsVO> exUtil = new ExportExcelUtil<EaoXlsVO>();
		String[] headers = { "所属一级账号", "项目名称", "建设地点", "报告类型", "项目负责人", "创建时间", "结束时间", "环评合同额" };
		List<EaoXlsVO> dataset = new ArrayList<>();
		DataTable dt = new Q(
				"select FirstNumber as FullName,p.ProjectName,BuildAddress,ReportType,FullName as PerjectLeader,PerjectTime,EntTime,ContractAmount from eao_project_info as p left join  Mh_Resource as m on p.PerjectLeader= m.Member LEFT JOIN (select ProjectNumber,ProjectName,m.Company,c.FullName as FirstNumber from eao_project_info as p LEFT JOIN Mh_Resource as m ON p.CreatePerson = m.Member LEFT JOIN Crm_Company as c on m.Company = c.ID where IsHide = 0 AND PerjectTime > DATE_SUB(CURDATE(), INTERVAL 6 MONTH) ORDER BY PerjectTime DESC)as cc on p.ProjectNumber=cc.ProjectNumber where IsHide = 0 AND PerjectTime > DATE_SUB(CURDATE(), INTERVAL 6 MONTH) ORDER BY PerjectTime DESC")
						.fetch();
		for (DataRow dataRow : dt) {
			EaoXlsVO vo = new EaoXlsVO();
			vo.setId(dataRow.getString("FullName"));
			vo.setPname(dataRow.getString("ProjectName"));
			vo.setBuildaddress(dataRow.getString("BuildAddress"));
			vo.setType(dataRow.getString("ReportType"));
			vo.setLeader(dataRow.getString("PerjectLeader"));
			vo.setCreate(dataRow.getString("PerjectTime"));
			vo.setEnd(dataRow.getString("EntTime"));
			vo.setTotal(dataRow.getString("ContractAmount"));
			dataset.add(vo);
		}
		String uuid =getUUID();
		String outputUrl =Url+ "download/"+uuid+".docx";
		OutputStream out;
		try {
			out = new FileOutputStream(outputUrl);
			exUtil.exportExcelUtil("测试", headers, dataset, out);
			out.close();
			System.out.println("导出成功！");
		} catch (IOException e) {
			e.printStackTrace();
		}
		download(za,outputUrl,"项目记录","xls");
	}
	/**
	 * 自动方法
	 * @param pid 项目编号
	 * @param inputUrl	模板路径
	 * @param outputUrl	输出路径
	 * @param testList	表格插入集合
	 */
	public void auto(String pid, String inputUrl, String outputUrl,List<List<String[]>> testList) {

		Map<String, String> testMap = printutil.getMap(pid);

		boolean changeFlag = true;
		try {
			// 获取docx解析对象
			XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(inputUrl));
			// 解析替换文本段落对象
			XwpfTUtils.changeText(document, testMap);
			// 解析替换表格对象
			XwpfTUtils.changeTable(document, testMap, testList);

			// 生成新的word
			File file = new File(outputUrl);
			FileOutputStream stream = new FileOutputStream(file);
			document.write(stream);
			stream.close();

		} catch (IOException e) {
			e.printStackTrace();
			changeFlag = false;
		}
		if (changeFlag) {
			s("打印成功");
		} else {
			f("打印失败");
		}
	}
	
	/**
	 * 自动方法审核表用
	 * @param pid 项目编号
	 * @param atype	审核类型
	 * @param inputUrl	模板路径
	 * @param outputUrl	输出路径
	 * @param testList	表格替换集合
	 */
	public void auto(String pid,String atype, String inputUrl, String outputUrl,List<List<String[]>> testList) {

		Map<String, String> testMap = printutil.getAudit(pid, atype);

		boolean changeFlag = true;
		try {
			// 获取docx解析对象
			XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(inputUrl));
			// 解析替换文本段落对象
			XwpfTUtils.changeText(document, testMap);
			// 解析替换表格对象
			XwpfTUtils.changeTable(document, testMap, testList);

			// 生成新的word
			File file = new File(outputUrl);
			FileOutputStream stream = new FileOutputStream(file);
			document.write(stream);
			stream.close();

		} catch (IOException e) {
			e.printStackTrace();
			changeFlag = false;
		}
		if (changeFlag) {
			s("打印成功");
		} else {
			f("打印失败");
		}
	}
	/**
	 * 自动方法  只有替换方法
	 * @param pid 项目编号
	 * @param inputUrl	模板路径
	 * @param outputUrl	输出路径
	 */
	public void auto(String pid,String inputUrl, String outputUrl) {
		
		Map<String, String> testMap = printutil.getMap(pid);
		
		boolean changeFlag = true;
		try {
			// 获取docx解析对象
			XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(inputUrl));
			// 解析替换文本段落对象
			XwpfTUtils.changeText(document, testMap);
			// 解析替换表格对象
			XwpfTUtils.changeTable3(document, testMap);
			
			// 生成新的word
			File file = new File(outputUrl);
			FileOutputStream stream = new FileOutputStream(file);
			document.write(stream);
			stream.close();
			
		} catch (IOException e) {
			e.printStackTrace();
			changeFlag = false;
		}
		if (changeFlag) {
			s("打印成功");
		} else {
			f("打印失败");
		}
	}
	/**
	 * 自动方法2	表格插入不含表格替换
	 * @param pid 项目编号
	 * @param inputUrl	模板路径
	 * @param outputUrl	输出路径
	 * @param testList	表格替换集合
	 */
	public void auto2(String pid, String inputUrl, String outputUrl,List<List<String[]>> testList) {

		Map<String, String> testMap = printutil.getMap(pid);

		boolean changeFlag = true;
		try {
			// 获取docx解析对象
			XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(inputUrl));
			// 解析替换文本段落对象
			XwpfTUtils.changeText(document, testMap);
			// 解析替换表格对象
			XwpfTUtils.changeTable2(document, testMap, testList);

			// 生成新的word
			File file = new File(outputUrl);
			FileOutputStream stream = new FileOutputStream(file);
			document.write(stream);
			stream.close();

		} catch (IOException e) {
			e.printStackTrace();
			changeFlag = false;
		}
		if (changeFlag) {
			s("打印成功");
		} else {
			f("打印失败");
		}
	}

	/**
	 * 审核人列表map
	 * @param startTime
	 * @param endTime
	 * @param inputUrl
	 * @param outputUrl
	 * @param testList
	 */
	public void auto3(String startTime, String endTime, String inputUrl, String outputUrl,List<List<String[]>> testList) {
		
		Map<String, String> testMap = printutil.getAuditorMap(startTime, endTime);
		
		boolean changeFlag = true;
		try {
			// 获取docx解析对象
			XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(inputUrl));
			// 解析替换文本段落对象
			XwpfTUtils.changeText(document, testMap);
			// 解析替换表格对象
			XwpfTUtils.changeTable(document, testMap, testList);

			// 生成新的word
			File file = new File(outputUrl);
			FileOutputStream stream = new FileOutputStream(file);
			document.write(stream);
			stream.close();
			
		} catch (IOException e) {
			e.printStackTrace();
			changeFlag = false;
		}
		if (changeFlag) {
			s("打印成功");
		} else {
			f("打印失败");
		}
	}
	/**
	 * 下载文件
	 * @param za
	 * @param filePath 目标路径
	 * @param fileName	文件名
	 * @param fileSuffix	文件后缀
	 */
	public void download(EAction za,String filePath,String fileName,String fileSuffix) {
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(filePath);
			IOUtil.download(za.getRequest(), za.getResponse(),
					fileName + "." + fileSuffix, fis);
		} catch (Exception e) {
			e.printStackTrace();
			za.writeHTML("File not found:" + filePath);
		} finally {
			try {
				if (fis != null) {
					fis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		File file=new File(filePath);
        if(file.exists()&&file.isFile())
            file.delete();
		return;
	}
	
}
